import Vue from 'vue'
import VueRouter from 'vue-router'

import Home from '@/views/Home'
import Search from '@/views/Search'
import Login from '@/views/Login'
import Register from '@/views/Register'
// 在路由中就进行使用
Vue.use(VueRouter)
let originPush = VueRouter.prototype.push;
let originReplace = VueRouter.prototype.push;
// 重写push和replace方法
// 第一个参数：告诉原来的push方法，往哪传参
// 第二个参数：告诉原来的push方法，成功的回调
// 第三个参数：告诉原来的push方法，失败的回调
// 
VueRouter.prototype.push = function (location, resolve, reject) {
    if (resolve && reject) {
        // call和apply的区别是call种传递多个参数使用逗号分隔，而apply则使用数组进行传递
        originPush.call(this, location, resolve, reject)
    } else {
        originPush.call(this, location, () => { }, () => { })
    }
}
VueRouter.prototype.replace = function (location, resolve, reject) {
    if (resolve && reject) {
        // call和apply的区别是call种传递多个参数使用逗号分隔，而apply则使用数组进行传递
        originReplace.call(this, location, resolve, reject)
    } else {
        originReplace.call(this, location, () => { }, () => { })
    }
}
export default new VueRouter({
    routes: [
        {
            name: 'home',
            path: '/home',
            component: Home,
            meta: {
                show: true
            }
        },
        {
            name: 'search',
            path: '/search/:keyword?',
            component: Search,
            meta: {
                show: true
            },
            props: true
        },
        {
            name: 'login',
            path: '/login',
            component: Login,
            meta: {
                show: false
            }
        },
        {
            name: 'register',
            path: '/register',
            component: Register,
            meta: {
                show: false
            }
        },
        {
            // 重定向
            path: '*',
            redirect: '/home'
        }

    ]
})
